package com.rk.study01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        // D:\hadoop_study_data\study01\input\hello.txt D:\hadoop_study_data\study01\output
        args = new String[]{"D:\\hadoop_study_data\\study01\\test_combine", "D:\\hadoop_study_data\\study01\\output2"};

        // 1.获取配置信息及封装的job任务
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2.设置jar加载路径
        job.setJarByClass(WordCountDriver.class);

        // 3.设置map和reducer类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        // 4.设置map输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5.设置最终输出的k、v类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 如果不设置InputFormat，它默认用的是TextInputFormat.class
        job.setInputFormatClass(CombineTextInputFormat.class);
        //虚拟存储切片最大值设置4m
        CombineTextInputFormat.setMaxInputSplitSize(job, 1024*1024*4);

        // 6.设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));


        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7.提交
        boolean result = job.waitForCompletion(true);
        System.exit(result? 0: 1);
    }
}
